const mongoose = require('mongoose');
const Shop = require('../src/models/shop.model');
const User = require('../src/models/user.model');

// 连接数据库
const connectDB = async () => {
  try {
    await mongoose.connect('mongodb+srv://goshop:goshop123@cluster0.hic1spu.mongodb.net/goshop?retryWrites=true&w=majority');
    console.log('✅ MongoDB数据库连接成功');
  } catch (error) {
    console.error('❌ MongoDB连接失败:', error);
    process.exit(1);
  }
};

// 创建测试店铺数据
const createTestShops = async () => {
  try {
    // 首先查找或创建一个测试用户
    let testUser = await User.findOne({ email: 'test@example.com' });
    if (!testUser) {
      testUser = await User.create({
        name: '测试用户',
        email: 'test@example.com',
        password: '123456',
        isAdmin: false,
        role: 'customer'
      });
      console.log('✅ 创建测试用户成功');
    }

    // 创建测试店铺数据
    const testShops = [
      {
        name: '小米官方旗舰店',
        description: '小米官方旗舰店，提供最新的小米手机、笔记本电脑、智能家居产品等。正品保证，全国联保。',
        logo: 'https://via.placeholder.com/200x200/ff6900/ffffff?text=小米',
        banner: 'https://via.placeholder.com/800x300/ff6900/ffffff?text=小米官方旗舰店',
        owner: testUser._id,
        type: 'flagship',
        status: 'active',
        categories: ['手机数码', '电脑办公', '智能家居'],
        tags: ['官方', '正品', '全国联保'],
        contact: {
          phone: '400-100-5678',
          email: 'service@mi.com',
          address: '北京市海淀区清河中街68号华润五彩城',
          workingHours: '周一至周日 9:00-21:00'
        },
        announcement: '双11大促销，全场8折起！新品首发，限时抢购！',
        rating: {
          overall: 4.8,
          service: 4.9,
          logistics: 4.7,
          description: 4.8
        },
        reviewCount: 12580,
        certification: {
          isVerified: true,
          verifiedAt: new Date()
        },
        stats: {
          totalProducts: 156,
          totalSales: 25680,
          totalOrders: 8960,
          monthlyRevenue: 2580000,
          followers: 89560
        }
      },
      {
        name: '华为官方商城',
        description: '华为官方商城，华为手机、平板、笔记本、智能穿戴设备官方直营。品质保证，售后无忧。',
        logo: 'https://via.placeholder.com/200x200/ff0000/ffffff?text=华为',
        banner: 'https://via.placeholder.com/800x300/ff0000/ffffff?text=华为官方商城',
        owner: testUser._id,
        type: 'official',
        status: 'active',
        categories: ['手机数码', '电脑办公', '智能穿戴'],
        tags: ['官方', '正品', '全球联保'],
        contact: {
          phone: '400-822-9999',
          email: 'service@huawei.com',
          address: '深圳市龙岗区坂田华为基地',
          workingHours: '7x24小时在线服务'
        },
        announcement: 'Mate系列新品上市，预约享受专属优惠！',
        rating: {
          overall: 4.9,
          service: 4.8,
          logistics: 4.9,
          description: 4.9
        },
        reviewCount: 18960,
        certification: {
          isVerified: true,
          verifiedAt: new Date()
        },
        stats: {
          totalProducts: 203,
          totalSales: 35680,
          totalOrders: 12560,
          monthlyRevenue: 4580000,
          followers: 156890
        }
      },
      {
        name: '数码小店',
        description: '专业数码产品销售，提供各种手机配件、数码周边产品。价格实惠，质量保证。',
        logo: 'https://via.placeholder.com/200x200/0066cc/ffffff?text=数码',
        banner: 'https://via.placeholder.com/800x300/0066cc/ffffff?text=数码小店',
        owner: testUser._id,
        type: 'personal',
        status: 'pending',
        categories: ['手机配件', '数码周边'],
        tags: ['实惠', '质量保证'],
        contact: {
          phone: '138-0013-8888',
          email: 'shop@digital.com',
          address: '广东省深圳市南山区科技园',
          workingHours: '周一至周六 9:00-18:00'
        },
        announcement: '新店开业，全场9折优惠！',
        rating: {
          overall: 4.2,
          service: 4.1,
          logistics: 4.3,
          description: 4.2
        },
        reviewCount: 156,
        certification: {
          isVerified: false
        },
        stats: {
          totalProducts: 45,
          totalSales: 680,
          totalOrders: 256,
          monthlyRevenue: 15600,
          followers: 89
        }
      },
      {
        name: '时尚服装店',
        description: '时尚女装专营店，提供最新潮流服装。款式新颖，质量上乘，价格合理。',
        logo: 'https://via.placeholder.com/200x200/ff69b4/ffffff?text=时尚',
        banner: 'https://via.placeholder.com/800x300/ff69b4/ffffff?text=时尚服装店',
        owner: testUser._id,
        type: 'personal',
        status: 'suspended',
        categories: ['服装鞋帽', '女装'],
        tags: ['时尚', '潮流', '女装'],
        contact: {
          phone: '139-2468-1357',
          email: 'fashion@style.com',
          address: '上海市黄浦区南京东路',
          workingHours: '周一至周日 10:00-22:00'
        },
        announcement: '春季新品上市，买二送一！',
        rating: {
          overall: 4.0,
          service: 3.9,
          logistics: 4.1,
          description: 4.0
        },
        reviewCount: 568,
        certification: {
          isVerified: false
        },
        stats: {
          totalProducts: 89,
          totalSales: 1256,
          totalOrders: 456,
          monthlyRevenue: 45600,
          followers: 234
        }
      }
    ];

    // 清除现有的测试数据
    await Shop.deleteMany({ name: { $in: testShops.map(shop => shop.name) } });
    console.log('🗑️ 清除现有测试数据');

    // 插入新的测试数据
    const createdShops = await Shop.insertMany(testShops);
    console.log(`✅ 成功创建 ${createdShops.length} 个测试店铺`);

    // 显示创建的店铺信息
    createdShops.forEach(shop => {
      console.log(`📦 ${shop.name} (${shop._id}) - ${shop.status}`);
    });

  } catch (error) {
    console.error('❌ 创建测试店铺失败:', error);
  }
};

// 主函数
const main = async () => {
  await connectDB();
  await createTestShops();
  
  console.log('\n🎉 测试数据创建完成！');
  console.log('现在可以在管理后台测试店铺管理功能了。');
  
  process.exit(0);
};

// 运行脚本
main().catch(error => {
  console.error('❌ 脚本执行失败:', error);
  process.exit(1);
});
